package sanrenxing.dayEnd.backRepertoryTotal.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sanrenxing.dayEnd.backRepertoryTotal.dao.MoneyitiesDao;
import sanrenxing.dayEnd.backRepertoryTotal.domain.CashReceivable;
import sanrenxing.dayEnd.backRepertoryTotal.service.MoneyitiesService;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @desrciption 现金应收应付 业务处理
 * @auth：yidu 李小锋
 * @date:2021/3/13 0013 - 16:32
 * @version:1:0
 */
@Service("MoneyitiesService")
public class MoneyitiesServiceImpl implements MoneyitiesService {
    //自动装配
    @Autowired
    private MoneyitiesDao moneyitiesDao;

    /**
     * 新增一行数据
     *
     * @param cashReceivable 对象
     * @return 0、增加失败 1、增加成功
     */
    @Override
    public int add(CashReceivable cashReceivable) {
        return moneyitiesDao.add(cashReceivable);
    }

    /**
     * 删除一行数据
     *
     * @param MoneyId 编号
     * @return 0、删除失败 1、删除成功
     */
    @Override
    public int delete(int MoneyId) {
        return moneyitiesDao.delete(MoneyId);
    }

    /**
     * 修改一行数据
     *
     * @param cashReceivable 对象
     * @return 0、修改失败 1、修改成功
     */
    @Override
    public int update(CashReceivable cashReceivable) {
        return moneyitiesDao.update(cashReceivable);
    }

    /**
     * 查询所有的订单数据
     *
     * @return 返回一个订单集合
     */
    @Override
    public List<CashReceivable> findAll() {
        return moneyitiesDao.findAll();
    }

    /**
     * 分页查询
     *
     * @param start     开始行号
     * @param rows      每页行数
     * @param condition 查询条件
     * @return 集合
     */
    @Override
    public List<CashReceivable> findByPage(int start, int rows, String condition) {
        int pages=((rows-1)*start)+1;
        return moneyitiesDao.findByPage(pages,start+pages,condition);
    }

    /**
     * 按条件统计出记录行数
     *
     * @param condition 条件
     * @return 记录行数
     */
    @Override
    public int count(String condition) {
        return moneyitiesDao.count(condition);
    }

    /**
     * 获取查询条件
     *
     * @param moneyities 对象
     * @return 条件字符串
     */
    @Override
    public String getcondition(CashReceivable moneyities) {
        String condition= " ";
        if(moneyities==null){
            return condition;
        }
        //id	主键编号	int
        if(moneyities.getMoneyId()!=0){
            condition+="and MoneyId '%"+moneyities.getMoneyId()+"%'";
        }
        //businDate	业务日期	date
        if(moneyities.getBusinDate()!=null){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            condition+="and businDate like to_date('"+format.format(moneyities.getBusinDate())+"','yyyy-MM-dd')";
        }
        return condition;
    }

    /**
     * 删除现金应收应付需要反冲销的数据
     * @return
     */
    @Override
    public boolean washDelete() {
        try {
            //调用方法获得需要反冲销数据的id
            Map<String,Object> wash=null;
            while(moneyitiesDao.washDelete()!=null && (wash=moneyitiesDao.washDelete()).size()!=0){
                for (String key : wash.keySet()) {
                    BigDecimal b = (BigDecimal)wash.get(key);
                    String s2 = b.toString();
                    //调用删除现金应收应付的方法：根据id删除数据
                    moneyitiesDao.delete(Integer.parseInt(s2));
                }
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
